package com.cgw.auth.enumer;

import com.cgw.auth.constance.AuthConst;
import org.apache.commons.lang3.StringUtils;
import org.redis.service.RedisKeys;

import java.util.Map;
import java.util.WeakHashMap;

/**
 * BankRedisKey
 *
 * @author grl
 * @date 2024/4/3
 */
public enum AuthRedisKey implements RedisKeys {
    /**
     *  授权详情信息
     */
    AUTH_DETAILS("auth:details",-1L,"","1");

    /**
     * key的前缀
     */
    private String prefix;
    /**
     * 约定的时效 单位秒
     */
    private Long time;

    private String key;

    private Object value;

    AuthRedisKey(String prefix,Long time, String key, Object value) {
        this.prefix = prefix;
        this.time = time;
        this.key = key;
        this.value = value;
    }

    /**
     * 推荐使用
     *
     * @param key   使用默认则传null
     * @param value 使用默认则传null
     */
    public void set(String key, Object value) {
        Map<String, Object> localMap = threadLocal.get();
        if (localMap == null) {
            localMap = new WeakHashMap<>();
            threadLocal.set(localMap);
        }
        if (StringUtils.isNotBlank(key)) {
            localMap.put(this.prefix + ":key", key);
        }
        if (value != null) {
            localMap.put(this.prefix + ":value", value);
        }
    }

    /**
     * 推荐使用
     *
     * @param value 保存的值 使用默认则传null
     * @param keys  key 传多个 最终的效果 如： key1:key2:key3 使用默认则传null
     */
    public void join(Object value, String... keys) {
        if (keys == null) {
            set(null, value);
        } else {
            set(String.join(":", keys), value);
        }
    }

    /**
     * 设置多层key
     *
     * @param keys
     * @return
     */
    public AuthRedisKey joinKey(String... keys) {
        return setKey(String.join(":", keys));
    }

    /**
     * 设置key
     *
     * @param key
     * @return
     */
    public AuthRedisKey setKey(String key) {
        Map<String, Object> localMap = threadLocal.get();
        if (localMap == null) {
            localMap = new WeakHashMap<>();
            threadLocal.set(localMap);
        }
        localMap.put(this.prefix + ":key", key);
        return this;
    }


    /**
     * 设置 value 值
     *
     * @param value
     * @return
     */
    public AuthRedisKey setValue(Object value) {
        Map<String, Object> localMap = threadLocal.get();
        if (localMap == null) {
            localMap = new WeakHashMap<>();
            threadLocal.set(localMap);
        }
        localMap.put(this.prefix + ":value", value);
        return this;
    }

    @Override
    public Long getTime() {
        Map<String, Object> localMap = threadLocal.get();
        if (localMap != null) {
            Long time = (Long) localMap.get(this.prefix + ":time");
            if (time != null) {
                return time;
            }
        }
        return this.time;
    }

    /**
     * 获取一个 key
     *
     * @return 项目标识:前缀:#{key}
     */
    @Override
    public String getKey() {
        StringBuilder sb = new StringBuilder(80);
        sb.append(AuthConst.PROJECT_NAME)
                .append(":")
                .append(this.prefix);
        Map<String, Object> localMap = threadLocal.get();
        if (localMap != null) {
            String key = (String) localMap.get(this.prefix + ":key");
            if (StringUtils.isNotBlank(key)) {
                sb.append(":").append(key);
            }
        }
        return sb.toString();
    }


    @Override
    public Object getValue() {
        Map<String, Object> localMap = threadLocal.get();
        if (localMap != null) {
            Object value = localMap.get(this.prefix + ":value");
            if (value != null) {
                return value;
            }
        }
        return this.value;
    }
}
